<template>
  <div class="table">
    <div class="container">
      <div class="handle-box">
        <el-button type="primary" size="mini" @click="deleteAll"
          >批量删除</el-button
        >
        &nbsp;
        <el-input
          v-model="select_word"
          size="mini"
          placeholder="请输入id"
          class="handle-input"
        ></el-input>
        &nbsp;
        <el-input
          v-model="select_word1"
          size="mini"
          placeholder="请输入标签名"
          class="handle-input"
        ></el-input>
        &nbsp;
        <el-button type="primary" size="mini" @click="getData"
          >查询标签</el-button
        >
        <el-button type="primary" size="mini" @click="show">添加标签</el-button>
      </div>
    </div>
    <el-table
      size="mini"
      ref="multipleTable"
      border
      style="width: 100%"
      height="680px"
      :data="tableData"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="45"></el-table-column>
      <el-table-column
        prop="id"
        label="id"
        width="400"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="tagName"
        label="标签名"
        width="400"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="updateTime"
        label="更新时间"
        width="400"
        align="center"
      ></el-table-column>
      <el-table-column label="操作" width="400" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deleteRow(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        background
        layout="total,prev,pager,next"
        :current-page="currentPage"
        :page-size="pageSize"
        :total="total"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </div>
    <el-dialog
      title="添加标签"
      :visible.sync="centerDialogVisible"
      width="400px"
      center
    >
      <el-form
        :model="registerForm"
        ref="registerForm"
        label-width="80px"
        :rules="rules"
      >
        <el-form-item prop="tagName" label="标签名" size="mini">
          <el-input
            v-model="registerForm.tagName"
            placeholder="标签名"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button size="mini" @click="centerDialogVisible = false"
          >取消</el-button
        >
        <el-button size="mini" @click="addTag">确定</el-button>
      </span>
    </el-dialog>

    <el-dialog
      title="修改标签"
      :visible.sync="editVisible"
      width="400px"
      center
    >
      <el-form :model="form" ref="form" label-width="80px" :rules="rules">
        <el-form-item prop="tagName" label="标签名" size="mini">
          <el-input v-model="form.tagName" placeholder="标签名"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button size="mini" @click="editVisible = false">取消</el-button>
        <el-button size="mini" @click="editTag">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      centerDialogVisible: false, //添加弹窗是否显示
      editVisible: false, //编辑弹窗是否显示
      delVisible: false, //删除弹窗是否显示
      registerForm: {
        //添加框
        tagName: "",
      },
      form: {
        //编辑框
        id: "",
        tagName: "",
      },
      tableData: [],
      tempData: [],
      select_word: "",
      select_word1: "",
      pageSize: 5, //分页每页大小
      currentPage: 1, //当前页
      total: 0,
      ids: -1, //当前选择项
      multipleSelection: [], //哪些项已经打勾
      rules: {
        tagName: [{ required: true, message: "请输入标签名", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getData();
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    resetForm() {
      this.registerForm = {};
    },
    show() {
      this.resetForm();
      this.centerDialogVisible = true;
    },
    //获取当前页
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getData();
    },
    getData() {
      this.tempData = [];
      this.tableData = [];
      let param =
        "tag/" +
        this.currentPage +
        "/" +
        this.pageSize +
        "?id=" +
        this.select_word +
        "&tagName=" +
        this.select_word1;
      this.$get(param).then((res) => {
        if (res.code == 200) {
          this.tableData = res.data.records;
          this.tempData = res.data.records;
          this.total = res.data.total;
        }
      });
    },
    addTag() {
      this.$refs["registerForm"].validate((valid) => {
        if (valid) {
          let param = {
            tagName: this.registerForm.tagName,
          };
          this.$post("tag", param).then((res) => {
            if (res.code == 200) {
              this.getData();
              this.centerDialogVisible = false;
            } else {
              this.$message.error(res.message);
            }
          });
        } else {
          this.$message.error("数据校验失败，请检查!", "error");
        }
      });
    },
    //弹出编辑页面
    handleEdit(row) {
      this.editVisible = true;
      this.form = {
        id: row.id,
        tagName: row.tagName,
      };
    },
    //保存编辑页面修改的数据
    editTag() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          let param = {
            id: this.form.id,
            tagName: this.form.tagName,
          };
          this.$put("tag", param).then((res) => {
            if (res.code == 200) {
              this.getData();
              this.editVisible = false;
            } else {
              this.$message.error(res.message);
            }
          });
        } else {
          this.$message.error("数据校验失败，请检查!");
        }
      });
    },
    deleteRow(val) {
      this.$confirm("您确定要删除吗？", "提示", {
        type: "warning",
      }).then(() => {
        this.$delete("tag/" + val).then((res) => {
          if (res.code == 200) {
            this.getData();
          } else {
            this.$message.error(res.message);
          }
        });
      });
    },
    deleteAll() {
      this.$confirm("您确定要删除吗？", "提示", {
        type: "warning",
      }).then(() => {
        console.log(this.multipleSelection);
        for (var i = 0; i < this.multipleSelection.length; i++) {
          this.$delete("tag/" + this.multipleSelection[i].id).then((res) => {
            if (res.code == 200) {
              this.getData();
            } else {
              this.$message.error(res.message);
            }
          });
        }
      });
    },
  },
};
</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
  float: left;
}
.handle-input {
  width: 250px;
  display: inline-block;
}
.pagination {
  display: flex;
  justify-content: center;
}
</style>